import 'package:flutter/services.dart';

import 'audio_data_platform_interface.dart';

class AudioData {
  static const EventChannel _eventChannel =
      EventChannel("audio_data_eventChannel");

  static final List<AudioDataStream> delegates = [];

  ///初始化
  static void init() {
    _eventChannel.receiveBroadcastStream().listen((list) {
      if (list is Uint8List) {
        Uint8List uint8list = list as Uint8List;

        for (var element in delegates) {
          element.onStream(uint8list);
        }
      }
    });
  }

  ///添加代理
  add(AudioDataStream stream) {
    if (delegates.contains(stream)) return;
    delegates.add(stream);
  }

  ///移除代理
  remove(AudioDataStream stream) {
    delegates.remove(stream);
  }

  ///开启录音
  Future<void> startRecord() {
    return AudioDataPlatform.instance.startRecord();
  }

  ///结束录音
  Future<void> stopRecord() {
    return AudioDataPlatform.instance.stopRecord();
  }
}

abstract class AudioDataStream {
  onStream(Uint8List? list);
}
